/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.chrfra.backing;
import edu.chl.chrfra.exceptions.NonexistentEntityException;
import edu.chl.chrfra.entities.Car;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
@ManagedBean
@RequestScoped
public class ViewProductsController {

    @ManagedProperty(value = "#{persistenceUtilityBean}")
    PersistenceUtilityBean pBean;
    
    public void setpBean(PersistenceUtilityBean pBean) {
        this.pBean = pBean;
    }

    /** Creates a new instance of viewProductsController */
    public ViewProductsController() {
    }

    public String emptyCart() {
        Object o = FacesContext.getCurrentInstance().
                getExternalContext().getSession(false);
        if (o != null) {
            ((HttpSession) o).invalidate();

            Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO,
                    "Session was invalidated!");
        }

        return "onEmptyCart";
    }

    public String addToCart(Car p) {
        Logger.getAnonymousLogger().log(Level.INFO, "Adding product " + p.getId().toString() + " to cart.");
        return "onViewProducts";
    }

    public String removeProduct(Car p){

        try {
            Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, p.getId().toString());
            pBean.getJpaCtrl().destroy(p.getId());
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).log(Level.INFO, ex.getMessage(), ex);
        }
        
        return "onViewProducts";
    }
}
